<?php

include_once 'dbconnect.php';
include_once 'function.general.php';
$sql = new ownedsql();

class userManagement
{

    function userForm()
    {
        global $_SESSION;

        if (isAdmin() == true) {
            //$_SESSION változók beolvasása, ha vannak, ha nincsenek alapértelmezés meghatározása

            if (!empty($_SESSION['uname'])) {
                $uname = $_SESSION['uname'];
            } else {
                $uname = 'felhasználó';
            }

            if (!empty($_SESSION['pass1'])) {
                $pass1 = $_SESSION['pass1'];
            } else {
                $pass1 = 'jelszó';
            }

            if (!empty($_SESSION['pass2'])) {
                $pass2 = $_SESSION['pass2'];
            } else {
                $pass2 = 'jelszó';
            }

            if (!empty($_SESSION['sname'])) {
                $sname = $_SESSION['sname'];
            } else {
                $sname = 'Vezetéknév';
            }

            if (!empty($_SESSION['fname'])) {
                $fname = $_SESSION['fname'];
            } else {
                $fname = 'Keresztnév';
            }

            if (!empty($_SESSION['email'])) {
                $email = $_SESSION['email'];
            } else {
                $email = 'email';
            }

            if (!empty($_SESSION['phone'])) {
                $phone = $_SESSION['phone'];
            } else {
                $phone = 'telefon';
            }

            //űrlapot felépítő tömb, alap értékekkel

            $form = array(array('fieldLabel' => 'Felhasználó név', 'fieldName' => 'uname',
                'type' => 'text', 'value' => $uname), array('fieldLabel' => 'Jelszó',
                'fieldName' => 'pass1', 'type' => 'password', 'value' => $pass1), array('fieldLabel' =>
                'Jelszó (ismét)', 'fieldName' => 'pass2', 'type' => 'password', 'value' => $pass2),
                array('fieldLabel' => 'Vezetéknév', 'fieldName' => 'sname', 'type' => 'text',
                'value' => $sname), array('fieldLabel' => 'Keresztnév', 'fieldName' => 'fname',
                'type' => 'text', 'value' => $fname), array('fieldLabel' => 'email', 'fieldName' =>
                'email', 'type' => 'text', 'value' => $email), array('fieldLabel' => 'Telefon',
                'fieldName' => 'phone', 'type' => 'text', 'value' => $phone));


            //az űrlap generálása

?>
<form method="POST" action="">
    <fieldset>
        <legend>Új felhasználó</legend>
        <table>
                <?php
            $i = 1;
            foreach ($form as $item) {
?>
        <tr><td id="label"><?php echo $item['fieldLabel'] ?>:</td><td id="input"><input type="<?php echo
                $item['type']; ?>" tabindex="<?php echo $i; ?>" value="<?php echo
                $item['value']; ?>" name="<?php echo $item['fieldName']; ?>" /></td></tr>
                        <?php
                $i++;
            }
?>
        <tr><td colspan="2" id="button"><input type="submit" name="submit" tabindex="<?php echo
            $i; ?>" value="Mentés" /></td></tr>
        </table>
    </fieldset>
</form>
           <?php
        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }
    }

    function saltPwd($valToSalt)
    {
        global $_SESSION;
        $salt = substr(md5(uniqid(rand(), true)), 0, 9);
        $salted = sha1($salt . sha1($valToSalt . $salt) . $salt);
        $_SESSION['salt'] = $salt;
        return $salted;
    }

    function clean_var($variable)
    {
        $variable = strip_tags(stripslashes(trim(rtrim(mysql_real_escape_string($variable)))));
        return $variable;
    }

    function validEmail($email)
    {
        $isValid = true;
        $atIndex = strrpos($email, "@");
        if (is_bool($atIndex) && !$atIndex) {
            $isValid = false;
        } else {
            $domain = substr($email, $atIndex + 1);
            $local = substr($email, 0, $atIndex);
            $localLen = strlen($local);
            $domainLen = strlen($domain);
            if ($localLen < 1 || $localLen > 64) {
                // local part length exceeded
                $isValid = false;
            } else
                if ($domainLen < 1 || $domainLen > 255) {
                    // domain part length exceeded
                    $isValid = false;
                } else
                    if ($local[0] == '.' || $local[$localLen - 1] == '.') {
                        // local part starts or ends with '.'
                        $isValid = false;
                    } else
                        if (preg_match('/\\.\\./', $local)) {
                            // local part has two consecutive dots
                            $isValid = false;
                        } else
                            if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
                                // character not valid in domain part
                                $isValid = false;
                            } else
                                if (preg_match('/\\.\\./', $domain)) {
                                    // domain part has two consecutive dots
                                    $isValid = false;
                                } else
                                    if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\",
                                        "", $local))) {
                                        // character not valid in local part unless
                                        // local part is quoted
                                        if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\", "", $local))) {
                                            $isValid = false;
                                        }
                                    }
            if ($isValid && function_exists('checkdnsrr')) {
                if (!(checkdnsrr($domain, "MX") || checkdnsrr($domain, "A"))) {
                    // domain not found in DNS
                    $isValid = false;
                }
            }
        }
        return $isValid;
    }
    
    function validatePhone($numberString) {
        if (preg_match('/(\+36){1}[\-](1|20|30|70){1}[\-](\d{3}){1}[\-](\d{4}){1}/', $numberString)) {
                $retval = true;
            } else {
                $retval = false;
            }
    }


    function newUserAction()
    {
        global $_SESSION;

        if (isAdmin() == true) {
            $uid = $_SESSION['newuid'];

?>
           <div id="userAction"><a href="usermanager.php?mode=edit&uid=<?php echo
            $uid; ?>">Szerkesztés</a> | <a href="usermanager.php?mode=profileimg&uid=<?php echo
            $uid; ?>">kép feltöltése</a> | <a href="usermanager.php?mode=assignpos&uid=<?php echo
            $uid; ?>">pozíció beállítása</a> | <a href="usermanager.php?mode=viewuser&uid=<?php echo
            $uid; ?>">megtekintés</a></div>
        <?
        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }
    }

    function createJobForm()
    {
        if (isAdmin() == true) {
?>
        <form method="POST" action="" >
            <fieldset>
                <legend>Új pozíció</legend>
                <table>
                    <tr>
                        <td id="label">Új pozíció neve:</td>
                        <td id="input"><input type="text" name="title" /></td>
                    </tr>
                    <tr>
                        <td id="button" colspan="2"><input type="submit" name="submit" value="Mentés" tabindex="2" /></td>
                    </tr>
                </table>
            </fieldset>
        </form>
        <?php
        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }

    }

    function createJobAction()
    {
        global $sql, $_POST;

        if (isAdmin() == true) {

            $title = $this->clean_var($_POST['title']);
            $_POST['title'] = null;

            $sql->connect();
            $sql->query("INSERT 3i_jobs (jobName, jobStatus) VALUES ('$title','1')");
            $sql->kill();
            echo "<div id=notice><b>" . $title . "</b> pozíció létrehozva<br>";
            $title = null;

?>
        <a href="usermanager.php?mode=newjob">Új pozíció létrehozása</a> | <a href="usermanager.php?mode=managejobs">Pozíciók kezelése</a> | <a href="usermanager.php?mode=manageaccess">Jogosultságok beállítása</a> | <a href="usermanager.php?mode=newuser">Új felhasználó létrehozása</a></div>
        <?php

        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }


    }

    function setJobForm($uid)
    {
        global $sql;

        if (isAdmin() == true) {
            $sql->connect();
            $sql->query("SELECT * FROM 3i_jobs WHERE jobStatus=1 ORDER BY jobName DESC");
            $result = $sql->query;
            $sql->kill();
            $_SESSION['newuid'] = $uid;
            $this->newUserAction();

?>
        <form method="post" action="" >
            <fieldset>
                <p>Pozíció: <select name="jobid">
                    <?php
            while ($item = mysql_fetch_array($result)) {
?>
                            <option value="<?php echo $item['jobid'] ?>"><?php echo
                $item['jobName'] ?></option>
                            <?php
            }
?>
                </select></p>
                <p>
                    <input type="hidden" name="uid" value="<?php echo $uid ?>" />
                    <input type="submit" name="submit" value="Mentés" />
                </p>
            </fieldset>
        </form>
        <?php

        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }


    }

    function setJobAction()
    {
        global $sql, $_POST;

        if (isAdmin() == true) {

            $jobid = $this->clean_var($_POST['jobid']);
            $uid = $this->clean_var($_POST['uid']);

            $_POST['jobid'] = null;
            $_POST['uid'] = null;

            $sql->connect();
            $sql->query("INSERT 3i_job_assoc (jobid,uid) VALUES ('$jobid','$uid')");
            $sql->kill();

            $jobid = null;
            $uid = null;

            $this->newUserAction();

        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }


    }

    function newUser()
    {
        global $sql, $_POST, $_SESSION;

        if (isAdmin() == true) {
            //$_POST Értékek tisztítása és átadás változóknak

            $uname = $this->clean_var($_POST['uname']);
            $pass1 = $this->clean_var($_POST['pass1']);
            $pass2 = $this->clean_var($_POST['pass2']);
            $sname = $this->clean_var($_POST['sname']);
            $fname = $this->clean_var($_POST['fname']);
            $email = $this->clean_var($_POST['email']);
            $phone = $this->clean_var($_POST['phone']);

            $var = array('uname', 'pass1', 'pass2', 'sname', 'fname', 'email', 'phone');

            foreach ($var as $item) {
                $_POST[$item] = null;
            }
            //a hiba okát tartalmazó üres változó
            $reason = '';

            //felhasználó név ellenőrzése

            $sql->connect();
            $sql->query("SELECT uName FROM 3i_usernames WHERE uName LIKE '$uname'");
            $result = $sql->query;
            $sql->kill();

            if (mysql_num_rows($result) > 0) {
                $unameTrue = false;
                $reason .= '<p id="formError">Már van ilyen felhasználó!</p>';
            } else {
                $unameTrue = true;
            }

            //ha érvényes az email cím és a pass1 és pass2 változók megegyeznek
            if ($this->validEmail($email)) {
                $emailTrue = true;
            } else {
                $emailTrue = false;
                $reason .= '<p id="formError">Hibás email cím!</p>';
            }

            if ($pass1 == $pass2) {
                $passTrue = true;
            } else {
                $passTrue = false;
                $reason .= '<p id="formError">Hibás jelszó!</p>';

            }
            
            if ($this->validatePhone($phone)) {
                $phoneTrue = true;
            } else {
                $phoneTrue = false;
                $reason .= "<p id=\"formError\">Nem megfelelő telefonszám: +36-1/20/30/70-XXX-XXXX</p>";
            }

            if ($unameTrue == false or $emailTrue == false or $passTrue == false or $phoneTrue==false) {
?><p id="formError">Hiba az űrlapon!</p><?php
                echo $reason;

                $_SESSION['uname'] = $uname;
                $_SESSION['pass1'] = $pass1;
                $_SESSION['pass2'] = $pass2;
                $_SESSION['sname'] = $sname;
                $_SESSION['fname'] = $fname;
                $_SESSION['email'] = $email;
                $_SESSION['phone'] = $phone;

                $this->userForm();
            } else {
                //sózza meg a jelszót tárolás elött
                $securedPass = $this->saltPwd($pass1);
                $spice = $_SESSION['salt'];
                $_SESSION['salt'] = null;

                //sql parancsok
                $sql->connect();
                $sql->query("INSERT 3i_usernames (uName, uPass, spice) VALUES ('$uname', '$securedPass', '$spice')");
                $sql->query("SELECT uid FROM 3i_usernames ORDER BY uid DESC LIMIT 1");
                $uid = $sql->query;
                $uid = mysql_fetch_array($uid);
                $uid = $uid['uid'];
                $sql->query("INSERT 3i_userdetails (uid,sName,fName,email,phone) VALUES ('$uid','$sname','$fname','$email','$phone')");
                $sql->kill();

                //post és session kiürítése

                foreach ($var as $item) {
                    $_POST[$item] = null;
                    $_SESSION[$item] = null;
                }

                //használt változók kiürítése
                $uname = null;
                $pass1 = null;
                $pass2 = null;
                $sname = null;
                $fname = null;
                $email = null;

                $sql->connect();
                $sql->query("SELECT uid FROM 3i_usernames ORDER BY uid DESC LIMIT 1");
                $uid = $sql->query;
                $uid = mysql_fetch_array($uid);
                $uid = $uid['uid'];
                $sql->kill();

                $_SESSION['newuid'] = $uid;

                $this->newUserAction();
            }

        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }


    }


    function editUser($uid)
    {
        global $sql;

        if (isAdmin() == true) {

            $sql->connect();
            $sql->query("SELECT * FROM 3i_usernames LEFT JOIN 3i_userdetails on 3i_userdetails.uid=3i_usernames.uid WHERE 3i_usernames.uid='$uid' ");
            $result = $sql->query;
            $sql->kill();

            while ($item = mysql_fetch_array($result)) {
                $uname = $item['uName'];
                $pass1 = "jelszó";
                $pass2 = "jelszó";
                $sname = $item['sName'];
                $fname = $item['fName'];
                $email = $item['email'];
                $phone = $item['phone'];
                $_SESSION['newuid'] = $uid;
            }

            //űrlapot felépítő tömb, alap értékekkel

            $form = array(array('fieldLabel' => 'Felhasználó név', 'fieldName' => 'uname',
                'type' => 'text', 'value' => $uname), array('fieldLabel' => 'Jelszó',
                'fieldName' => 'pass1', 'type' => 'password', 'value' => $pass1), array('fieldLabel' =>
                'Jelszó (ismét)', 'fieldName' => 'pass2', 'type' => 'password', 'value' => $pass2),
                array('fieldLabel' => 'Vezetéknév', 'fieldName' => 'sname', 'type' => 'text',
                'value' => $sname), array('fieldLabel' => 'Keresztnév', 'fieldName' => 'fname',
                'type' => 'text', 'value' => $fname), array('fieldLabel' => 'email', 'fieldName' =>
                'email', 'type' => 'text', 'value' => $email), array('fieldLabel' => 'Telefon',
                'fieldName' => 'phone', 'type' => 'text', 'value' => $phone));


            //az űrlap generálása
            $this->newUserAction();
?>
<form method="POST" action="">
    <fieldset>
        <legend>Felhasználó szerkeszétse</legend>
        <table>
                <?php
            $i = 1;
            foreach ($form as $item) {
                if ($item['fieldName'] == 'uname') {
?>
                <tr><td id="label"><input type="hidden" name="uid"  value="<?php echo
                    $uid ?>"/>
        <?php echo $item['fieldLabel'] ?>:</td><td id="input"><?php echo $item['value']; ?></td></tr>
                <?php
                } else {
?>
        <tr><td id="label">
        <?php echo $item['fieldLabel'] ?>:</td><td id="input"><input type="<?php echo
                    $item['type']; ?>" tabindex="<?php echo $i; ?>" value="<?php echo
                    $item['value']; ?>" name="<?php echo $item['fieldName']; ?>" /></td></tr>
                        <?php
                    $i++;
                }
            }
?>
        <tr><td colspan="2" id="button"><input type="submit" name="submit" tabindex="<?php echo
            $i; ?>" value="Mentés" /></td></tr>
        </table>
    </fieldset>
</form>
           <?php

        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }
    }

    function editUserAction()
    {
        global $sql, $_POST, $_SESSION;

        if (isAdmin() == true) {
            //$_POST Értékek tisztítása és átadás változóknak

            $pass1 = $this->clean_var($_POST['pass1']);
            $pass2 = $this->clean_var($_POST['pass2']);
            $sname = $this->clean_var($_POST['sname']);
            $fname = $this->clean_var($_POST['fname']);
            $email = $this->clean_var($_POST['email']);
            $phone = $this->clean_var($_POST['phone']);
            $uid = $this->clean_var($_POST['uid']);
            $_SESSION['newuid'] = $uid;

            $var = array('pass1', 'pass2', 'sname', 'fname', 'email', 'phone', 'uid');

            foreach ($var as $item) {
                $_POST[$item] = null;
            }
            //a hiba okát tartalmazó üres változó
            $reason = '';


            //ha érvényes az email cím és a pass1 és pass2 változók megegyeznek
            if ($this->validEmail($email)) {
                $emailTrue = true;
            } else {
                $emailTrue = false;
                $reason .= '<p id="formError">Hibás email cím!</p>';
            }

            if ($pass1 != "jelszó" or $pass2 != "jelszó") {
                $changePass = true;
                if ($pass1 == $pass2) {
                    $passTrue = true;
                } else {
                    $passTrue = false;
                    $reason .= '<p id="formError">Hibás jelszó!</p>';

                }
            } else {
                $changePass = false;
                $passTrue = true;
            }


            if ($emailTrue == false or $passTrue == false) {
?><p>Hiba az űrlapon!</p><?php
                echo $reason;

                $this->editUser();
            } else {
                $sql->connect();

                //ha van jelszó változás
                if ($changePass == true) {
                    //sózza meg a jelszót tárolás elött
                    $securedPass = $this->saltPwd($pass1);
                    $spice = $_SESSION['salt'];
                    $_SESSION['salt'] = null;
                    $query = "UPDATE 3i_usernames SET uPass='$securedPass', spice='$spice' WHERE uid='$uid'";
                    $sql->query($query);

                }
                //jelszó kivételével frissítés
                $query = "UPDATE 3i_userdetails SET sName='$sname', fName='$fname', email='$email', phone='$phone' WHERE uid='$uid'";
                $sql->query($query);
                $sql->kill();

                //post és session kiürítése

                foreach ($var as $item) {
                    $_POST[$item] = null;
                    $_SESSION[$item] = null;
                }


                //használt változók kiürítése
                $uname = null;
                $pass1 = null;
                $pass2 = null;
                $sname = null;
                $fname = null;
                $email = null;

                $this->newUserAction();

            }

        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }
    }


    function overview()
    {
        global $sql;

        //TODO: felhasználó kisképe, új bal oszlop, colspan=2, vagy blank

        if (isAdmin() == true) {
            $sql->connect();
            $sql->query("SELECT * FROM 3i_userdetails LEFT JOIN 3i_job_assoc ON 3i_job_assoc.uid=3i_userdetails.uid LEFT JOIN 3i_jobs ON 3i_jobs.jobid=3i_job_assoc.jobid LEFT JOIN 3i_usernames ON 3i_usernames.uid=3i_userdetails.uid WHERE uActive='1' ORDER BY 3i_userdetails.uid ASC");
            $result = $sql->query;
            $sql->query("SELECT * FROM 3i_userdetails LEFT JOIN 3i_job_assoc ON 3i_job_assoc.uid=3i_userdetails.uid LEFT JOIN 3i_jobs ON 3i_jobs.jobid=3i_job_assoc.jobid LEFT JOIN 3i_usernames ON 3i_usernames.uid=3i_userdetails.uid WHERE uActive='0' ORDER BY 3i_userdetails.uid ASC");
            $result2 = $sql->query;
            $sql->kill();

            $activeNum = mysql_num_rows($result);
            $deletedNum = mysql_num_rows($result2);

?>
        <div id="title">Áttekintés | <a href="usermanager.php" style="text-decoration: underline;">Aktív (<?php echo
            $activeNum ?>)</a> - <a href="usermanager.php?mode=deletedusers">Törölt (<?php echo
                $deletedNum ?>)</a></div>
        
        <?php
            while ($row = mysql_fetch_array($result)) {
?>

            <table id="overViewTable">
            <tr>
                <td id="user" colspan="2"><span><a href="usermanager.php?mode=viewuser&uid=<?php echo
                $row['uid'] ?>"><?php echo $row['sName'] . " " . $row['fName'] .
                    "</a></span> - " . $row['jobName']; ?></td>
                <td id="action"><a href="usermanager.php?mode=edit&uid=<?php echo
                $row['uid'] ?>">Szerkesztés</a><?php if ($row['uid'] != 1) {
?>
                     | <a href="usermanager.php?mode=deleteuser&uid=<?php echo $row['uid'] ?>">Törlés</a></td>
                    <?php
                } ?>
            </tr>
            <tr>
                <td id="phone" rowspan="1" ><?php echo "Tel.: " . $row['phone']; ?></td>
                <td id="email" rowspan="1" ><?php echo "<a href=\"mailto:" . $row['email'] .
                "\">" . $row['email'] . "</a>" ?></td>
                <td id="action" rowspan="1" ><a href="usermanager.php?mode=assignpos&uid=<?php echo
                    $row['uid'] ?>">Pozíció</a> | <a href="usermanager.php?mode=assigndist&uid=<?php echo
                    $row['uid'] ?>">Kerület</a></td>
            </tr>
            </table>
<?php
            }

        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }


    }

    function deleteUser($uid)
    {
        if (isAdmin() == true) {
            global $sql;

            $sql->connect();
            $sql->query("UPDATE 3i_usernames SET uActive='0' WHERE uid='$uid'");
            $sql->query("UPDATE 3i_bp_district SET uid='0' WHERE uid='$uid'");
            $sql->kill();

            $this->overview();

        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }


    }


    function deletedUsers()
    {
        global $sql;

        //TODO: felhasználó kisképe, új bal oszlop, colspan=2, vagy blank

        if (isAdmin() == true) {
            $sql->connect();
            $sql->query("SELECT * FROM 3i_userdetails LEFT JOIN 3i_job_assoc ON 3i_job_assoc.uid=3i_userdetails.uid LEFT JOIN 3i_jobs ON 3i_jobs.jobid=3i_job_assoc.jobid LEFT JOIN 3i_usernames ON 3i_usernames.uid=3i_userdetails.uid WHERE uActive='0' ORDER BY 3i_userdetails.uid ASC");
            $result = $sql->query;
            $sql->query("SELECT * FROM 3i_userdetails LEFT JOIN 3i_job_assoc ON 3i_job_assoc.uid=3i_userdetails.uid LEFT JOIN 3i_jobs ON 3i_jobs.jobid=3i_job_assoc.jobid LEFT JOIN 3i_usernames ON 3i_usernames.uid=3i_userdetails.uid WHERE uActive='1' ORDER BY 3i_userdetails.uid ASC");
            $result2 = $sql->query;
            $sql->kill();

            $activeNum = mysql_num_rows($result2);
            $deletedNum = mysql_num_rows($result);

?>
        <div id="title">Áttekintés | <a href="usermanager.php">Aktív (<?php echo
            $activeNum ?>)</a> - <a href="usermanager.php?mode=deletedusers" style="text-decoration: underline;">Törölt (<?php echo
                $deletedNum ?>)</a></div>
        
        <?php
            while ($row = mysql_fetch_array($result)) {
?>
            <table id="overViewTable">
            <tr>
                <td id="user" colspan="2"><span><a href="usermanager.php?mode=viewuser&uid=<?php echo
                $row['uid'] ?>"><?php echo $row['sName'] . " " . $row['fName'] .
                    "</a></span> - " . $row['jobName']; ?></td><td id="action"><a href="usermanager.php?mode=edit&uid=<?php echo
                $row['uid'] ?>">Szerkesztés</a> | <a href="usermanager.php?mode=activate&uid=<?php echo
                    $row['uid'] ?>">Aktiválás</a></td>
            </tr>
            <tr>
                <td id="phone"><?php echo "Tel.: " . $row['phone']; ?></td><td id="email"><?php echo
                "<a href=\"mailto:" . $row['email'] . "\">" . $row['email'] . "</a>" ?></td><td id="action"><a href="usermanager.php?mode=assignpos&uid=<?php echo
                    $row['uid'] ?>">Pozíció</a> | Kerület</td>
            </tr>
            </table>
<?php
            }

        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }

    }

    function activateUser($uid)
    {
        if (isAdmin() == true) {
            global $sql;

            $sql->connect();
            $sql->query("UPDATE 3i_usernames SET uActive='1' WHERE uid='$uid'");
            $sql->kill();

            $this->deletedUsers();

        } else {
?>
            <div id="noAccess">Hozzáférés megtagadva!</div>
            <?php
        }

    }

    function viewUserDetail($uid)
    {
        global $sql;

        $sql->connect();
        $sql->query("SELECT * FROM 3i_userdetails WHERE uid='$uid'");
        $result = $sql->query;
        $sql->kill();

        while ($item = mysql_fetch_array($result)) {
?>
            <fieldset id="userFieldSet">
            <legend>Felhasználó - <a href="usermanager.php?mode=edit&uid=<?php echo
$uid; ?>">Szerk</a></legend>
            <table id="userDetail">
                <tr>
                    <td><img src="core/function.pimage.download.php?mode=full&uid=<?php echo
$uid ?>" /></td>
                    <td>
                        <table>
                            <tr>
                                <td>uid:</td>
                                <td><?php echo $uid ?></td>
                            </tr>
                            <tr>
                                <td>Név:</td>
                                <td><?php echo $item['sName'] . " " . $item['fName'] ?></td>
                            </tr>
                            <tr>
                                <td>@:</td>
                                <td><?php echo $item['email'] ?></td>
                            </tr>
                            <tr>
                                <td>tel.:</td>
                                <td><?php echo $item['phone'] ?></td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            </fieldset>
            <?php
        }
    }

    function listPositions()
    {
        global $sql;
        $sql->connect();
        $sql->query("SELECT * FROM 3i_jobs WHERE jobStatus='1' ORDER BY jobid ASC");
        $result = $sql->query;

        while ($item = mysql_fetch_array($result)) {
            $jobid = $item['jobid'];
?>
        <table id="userPositions">
            <tr>
                <th><?php echo $item['jobName'] ?></th>
            </tr>
            <tr>
                <td>
                    Betöltik: 
                    <?php
            $sql->query("SELECT group_concat((CONCAT(sName, ' ', fName)) SEPARATOR ', ') as nameList FROM 3i_userdetails LEFT JOIN 3i_job_assoc ON 3i_job_assoc.uid=3i_userdetails.uid WHERE 3i_job_assoc.jobid='$jobid'");
            $subResult = $sql->query;
            while ($subItem = mysql_fetch_array($subResult)) {
                echo $subItem['nameList'];
            }
?>
                </td>
            </tr>
        </table>
        <?php
        }

    }
    
    function assignUserDistrictForm($uid)
    {
        global $sql;

        $sql->connect();
        $sql->query("SELECT * FROM 3i_bp_district WHERE uid='0' OR uid='$uid'");
        $result = $sql->query;
        $sql->kill();

?>
        <form method="post" action="">
        <fieldset>
            <legend>Kerület hozzárendelés</legend>
            <table>
                <tr>
                    <td>
                    <?php
        while ($item = mysql_fetch_array($result)) {
?>
                        <div id="selBox"><?php echo $item['dName'] . " "; ?><input type="checkbox" value="<?php echo
$item['did'] ?>" name="distr[]" <?php if ($item['uid'] ==
$uid) { ?> checked="checked"  <?php } ?> /></div>
                        <?php
        }
?>
                    </td>
                </tr>
                <td>
                    <td>
                        <input type="hidden" value="<?php echo $uid ?>" name="uid" />
                        <input type="submit" name="assignSubmit" value="Mentés" />
                    </td>
                </td>
            </table>
        </fieldset>
        </form>
        <?php
    }

    function assignUserDistrictAction()
    
    {
        global $sql, $_POST;

        $uid = $_POST['uid'];
        $district = $_POST['distr'];

        $sql->connect();

        $query = "UPDATE 3i_bp_district SET uid='0' WHERE uid='$uid'";
        $sql->query($query);

        foreach ($district as $val) {
            $query = "UPDATE 3i_bp_district SET uid = '$uid' WHERE did='$val'";
            // echo $query . "<br />";
            $sql->query($query);
        }
        $sql->kill();

        echo "<div id=\"success\">Sikeres frissítés!</div>";
        
        $this->overview();
    }

}
?>